define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'jstree'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agent/index',
                    add_url: 'agent/add',
                    edit_url: 'agent/edit',
                    table: 'agent',
                },
                showExport:false
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: true,
                exportTypes: ['excel'],
                exportOptions: {
                    ignoreColumn: [0, 'operate'],
                    fileName: '代理商_'+ Moment().format("YYYY-MM-DD"),
                    onBeforeSaveToFile: function (data, fileName, type, charset, encoding, bom) {
                        return $.fn.bootstrapTable.defaults.extend.savestatus;
                    },
                    onCellHtmlHyperlink: function ($cell, rowIndex, colIndex, href, htmlData) {
                        var html = $.parseHTML(htmlData);
                        var inputidx = 0;
                        var selectidx = 0;
                        var result = '';

                        $.each(html, function () {
                            if ($(this).is("input")) {
                                result += $cell.find('input').eq(inputidx++).val();
                            } else if ($(this).is("select")) {
                                result += $cell.find('select option:selected').eq(selectidx++).text();
                            } else if ($(this).is("img")) {
                                var src = $(this).attr('src');
                                if (src.indexOf('http') === -1) {
                                    $(this).attr('src', window.location.origin + src);
                                }
                                $(this).attr("width", "40").attr("height", "40").attr('class', '');
                                result += '<div>' + $(this).get(0).outerHTML + '</div>';
                            } else {
                                if (typeof $(this).html() === 'undefined') {
                                    result += $(this).text();
                                } else if (jQuery().bootstrapTable === undefined ||
                                    ($(this).hasClass('filterControl') !== true &&
                                        $cell.parents('.detail-view').length === 0)) {
                                    result += $(this).html();
                                }
                            }
                        });
                        return result;
                    }
                },
                columns: [
                    [
                        // {field: 'districts', title: __('省、市、县区'), searchList: function (column) {
                        //         return Template('sourcetpl', {});
                        //     }, formatter: function (value, row, index) {
                        //         return '无';
                        //     }, visible: false
                        // },
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'avatar', title: '头像', formatter: Table.api.formatter.image, events: Table.api.events.image,operate: false},
                        {field: 'username', title: '账号',operate: 'like'},
                        {field: 'nickname', title: '名称',operate: 'like'},
                        {field: 'ratio', title: '佣金比例',operate: false,sortable: true,formatter: function (value) {
                                return (value * 100)+'%';
                            }},
                        {field: 'amap_province.name', title: '省份',operate: false},
                        {field: 'amap_city.name', title: '城市',operate: false},
                        {field: 'amap_districts.name', title: '区县',operate: false},
                        {field: 'address', title: '详细地址', operate: false},
                        {field: 'contacts', title: '联系人',operate: 'like'},
                        {field: 'mobile', title: '手机号',operate: 'like'},
                        {field: 'bind_qrcode', title: '绑定码', formatter: Table.api.formatter.image, events: Table.api.events.image, operate: false},
                        {field: 'member.member_name', title: '所属用户',operate: 'like'},
                        {field: 'status', title: __('status'), formatter: Table.api.formatter.status, searchList: {1: '正常', 0: '关闭'}},
                        {field: 'loginip', title: '最近登录IP',operate: false},
                        {field: 'logintime', title: '最近登录时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'createtime', title: __('createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});